/**
 * @param {number[]} arr
 * @return {number[]}
 */
export var sortByBits = function (arr) {
  return arr.sort((a, b) => {
    const a1 = find1Bits(a)
    const b1 = find1Bits(b)
    return a1 === b1 ? a - b : a1 - b1
  })
}

function find1Bits(num) {
  let count = 0
  while (num) {
    count += num & 1
    num >>= 1
  }
  return count
}
